"use strict";

$(function () {
  var token = localStorage.getItem("token");
  var id = localStorage.getItem("id");
  var username = localStorage.getItem("username"); //二维码

  if (!token && !id) {
    //表示你还未登录
    $(".islogin").hide();
    $(".notlogin").show();
  } else {
    $(".islogin").show();
    $(".notlogin").hide();
    $(".islogin_name").text(username);
  }

  $(".islogin_exit").click(function () {
    localStorage.removeItem("token");
    localStorage.removeItem("id");
    localStorage.removeItem("username");
    location.reload();
  });
  $(".islogin").hover(function () {
    $(".islogin_dorp").show();
  }, function () {
    $(".islogin_dorp").hide();
  });
  $("#ttbar-myjd").hover(function () {
    $(".dropdown-layer").show().css("z-index", 4);
  }, function () {
    $(".dropdown-layer").hide();
  });
  $("#ttbar-myjd1").hover(function () {
    $(".dropdown-layer1").show().css("z-index", 4);
  }, function () {
    $(".dropdown-layer1").hide();
  });

  if (token && id) {
    $.get("http://jx.xuzhixiang.top/ap/api/cart-list.php", {
      id: id
    }, function (res) {
      var str = "\n         <div class=\"cart-thead\">\n          <div class=\"t-check\">\n            <label for=\"checkboxs\">\n              <input type=\"checkbox\" id=\"checkboxs\" />\u5168\u9009</label\n            >\n          </div>\n          <p class=\"t-title\">\u5546\u54C1</p>\n          <p class=\"t-price\">\u5355\u4EF7</p>\n          <p class=\"t-num\">\u6570\u91CF</p>\n          <p class=\"t-pprices\">\u5C0F\u8BA1</p>\n          <p class=\"t-action\">\u64CD\u4F5C</p>\n        </div>\n        ";
      res.data.forEach(function (item) {
        str += "\n            <div class=\"cart-tbody\">\n          <div class=\"t-check\">\n              <input type=\"checkbox\" class=\"checkout\" data-id=\"".concat(item.pid, "\">\n          </div>\n          <p class=\"t-title\">\n            <img\n              src=\"").concat(item.pimg, "\"\n              alt=\"\"\n            />\n            <span>").concat(item.pdesc, "</span>\n          </p>\n          <p class=\"t-price\">\uFFE5").concat(item.pprice, "</p>\n          <p class=\"t-num clearfix\">\n              <button class=\"subtract\" data-id=\"").concat(item.pid, "\">-</button>\n              <input type=\"text\" class=\"sum\" data-id=\"").concat(item.pid, "\" value=\"").concat(item.pnum, "\">\n\n              <button class=\"add\" data-id=\"").concat(item.pid, "\">+</button>\n          </p>\n          <p class=\"t-pprices\">\uFFE5<span>").concat((item.pprice * item.pnum).toFixed(2), "</span></p>\n          <p class=\"t-action\" data-id=\"").concat(item.pid, "\">\u5220\u9664</p>\n        </div>\n            ");
      });
      $(".w").html(str); //多选总件数

      function zj() {
        var sums = $(".sum");
        var arr = [];

        for (var i = 0; i < sums.length; i++) {
          arr[i] = sums[i].value;
        }

        var num = 0;
        arr.forEach(function (item) {
          num += parseInt(item);
        }); //console.log(num);

        $(".amount-sum >b").text(num);
      } //
      //选中的总价函数


      function tatolprice() {
        if ($(".checkout:checked").length == 0) {
          $(".totalprice>b").text(0);
        }

        var nums = $(".checkout:checked").parent().siblings(".t-pprices").find("span"); //console.log($(".checkout:checked"));
        //console.log(nums);

        var arr = [];
        var totalprices = 0;

        for (var i = 0; i < nums.length; i++) {
          arr[i] = nums[i].innerText;
        }

        arr.forEach(function (item) {
          totalprices += +item;
        });
        $(".totalprice>b").text("￥" + totalprices.toFixed(2));
      } // 全选框


      $("#checkboxs").change(function () {
        zj();
        var flag = $(this).prop("checked");

        if (flag == false) {
          $(".amount-sum >b").text(0);
        }

        $(".checkout, #select-all").prop("checked", flag);
        tatolprice();
      });
      $("#select-all").change(function () {
        zj();
        var flag = $(this).prop("checked");

        if (flag == false) {
          $(".amount-sum >b").text(0);
        }

        $(".checkout, #checkboxs").prop("checked", flag);
        tatolprice();
      }); //单个复选框总件

      function zj2() {
        if ($(".checkout:checked").length == 0) {
          $(".amount-sum >b").text(0);
        }

        var sums = $(".checkout:checked").parent().siblings(".t-num").find(".sum");
        var arr = [];

        for (var i = 0; i < sums.length; i++) {
          arr[i] = sums[i].value;
        }

        var num = 0;
        arr.forEach(function (item) {
          num += parseInt(item);
        });
        $(".amount-sum >b").text(num);
      } //单个复选框


      $(".checkout").change(function () {
        zj2();
        tatolprice();

        if ($(".checkout").length === $(".checkout:checked").length) {
          $("#select-all, #checkboxs").prop("checked", true);
        } else {
          $("#select-all, #checkboxs").prop("checked", false);
        }
      });
      $(".subtract").click(function (e) {
        var _this = this;

        e.stopPropagation();
        var p = $(this).parent().siblings(".t-price").text();
        p = p.substr(1);
        var n = $(this).siblings(".sum").val();
        n--;

        if (n < 1) {
          n = 1;
          alert("最小数量不能为0");
        }

        console.log(n); // $(".totalprice>b").text(s);

        $.get("http://jx.xuzhixiang.top/ap/api/cart-update-num.php", {
          uid: id,
          pid: $(this).attr("data-id"),
          pnum: n
        }, function (res) {
          $(_this).siblings(".sum").val(n);
          var dz = (n * p).toFixed(2);
          $(_this).parent().siblings(".t-pprices").find("span").text(dz);
          zj2();
          tatolprice();

          if (n == 0) {}
        });
      });
      $(".add").click(function (e) {
        var _this2 = this;

        e.stopPropagation();
        var p = $(this).parent().siblings(".t-price").text();
        p = p.substr(1);
        var n = $(this).siblings(".sum").val();
        n++;
        0;
        $.get("http://jx.xuzhixiang.top/ap/api/cart-update-num.php", {
          uid: id,
          pid: $(this).attr("data-id"),
          pnum: n
        }, function (res) {
          $(_this2).siblings(".sum").val(n);
          var dz = (n * p).toFixed(2);
          $(_this2).parent().siblings(".t-pprices").find("span").text(dz);
          zj2();
          tatolprice();
        });
      });
      $(".sum").change(function () {
        var _this3 = this;

        var p = $(this).parent().siblings(".t-price").text();
        p = p.substr(1);
        var n = parseInt($(this).val());
        $(this).val(parseInt($(this).val())); //console.log(n)

        if (n <= 1) {
          $(this).val(1);
          n = 1;
          alert("最小数量不能为0");
        }
        

        $.get("http://jx.xuzhixiang.top/ap/api/cart-update-num.php", {
          uid: id,
          pid: $(this).attr("data-id"),
          pnum: n
        }, function (res) {
          $(_this3).siblings(".sum").val(n);
          var dz = (n * p).toFixed(2);
          $(_this3).parent().siblings(".t-pprices").find("span").text(dz);
          zj2();
          tatolprice();
        });
      });
      $(".t-action").click(function () {
        var _this4 = this;

        $.get("http://jx.xuzhixiang.top/ap/api/cart-delete.php", {
          uid: id,
          pid: $(this).attr("data-id")
        }, function (res) {
          $(_this4).parent().remove();
          tatolprice();
          zj2();

          if ($(".checkout").length === $(".checkout:checked").length) {
            $("#select-all, #checkboxs").prop("checked", true);
          } else {
            $("#select-all, #checkboxs").prop("checked", false);
          }
        });
      });
      $(".checkboxAll_remove").click(function () {
        var arr = [];
        var nums = $(".checkout:checked");

        for (var i = 0; i < nums.length; i++) {
          arr[i] = nums[i].getAttribute("data-id");
          nums[i].parentNode.parentNode.remove();
        }

        arr.forEach(function (item) {
          $.get("http://jx.xuzhixiang.top/ap/api/cart-delete.php", {
            uid: id,
            pid: item
          }, function (res) {
            tatolprice();
            zj2();
          });
        });

        if ($(".checkout:checked").length == 0) {
          $("#checkboxs,#select-all").prop("checked", false);
        }
      });
    });
  }
});